
let jobaddModule = (function(){
    let jobId = null,
    $inpBoxName = $('#inpBoxName'),
    $inpBoxDesc = $('#inpBoxDesc'),
    $spanName = $('.spanName'),
    $spanDesc = $('.spanDesc'),
    $userhandle = $('#userhandle'),
    $departhandle = $('#departhandle'),
    $jobhandle = $('#jobhandle'),
    $departcustomer = $('#departcustomer'),
    $allcustomer = $('#allcustomer'),
    $resetpassword = $('#resetpassword'),
    $submit = $('.submit');

    // 表单校验
    function checkName(){
        let val = $inpBoxName.val().trim();
        if(val.length===0){
            $spanName.html('当前是必填项');
            return false;
        }
        // 预防跨站简本攻击
        let reg = /<script>|<script\/>/g;
        if(reg.test(val)){
            val.replace(reg,' ');
        }
        $spanName.html('');
        return true;
    }
    function checkDesc(){
        let val = $inpBoxDesc.val().trim();
        if(val.length===0){
            $spanDesc.html('当前是必填项');
            return false;
        }
        // 预防跨站简本攻击
        let reg = /<script>|<script\/>/g;
        if(reg.test(val)){
            val.replace(reg,' ');
        }
        $spanDesc.html('');
        return true;
    }

    //提交用户信息 
    async function submitHandle(){
        if(!checkDesc()||!checkName()) return;
        let params = {
            name:$inpBoxName.val(),
            desc:$inpBoxDesc.val()
        };
        if(jobId){
            let result = await axios.post('/job/update',{
                name:params.name,
                desc:params.desc,
                jobId
            });
            if(result.code==0){
                alert('恭喜你，修改成功');
                $inpBoxName.val('');
                $inpBoxDesc.val('');
                return;
            }
        }else{
            let result = await axios.post('/job/add',params);
            if(result.code==0){
                alert('恭喜你，添加成功');
                $inpBoxName.val('');
                $inpBoxDesc.val('');
                return;
            }

        }
        alert('小主，当前网络繁忙，请您刷新后重试~~');
    }

    // 从编辑页跳过来=>获取信息至文本框
    async function queryBaseInfo(){
        let result = await axios.get('/job/info',{
            params:{
                jobId
            }
        });
        if(result.code==0){
            $inpBoxName.val(result.data.name);
            $inpBoxDesc.val(result.data.desc);
            return; 
        }
        alert('当前编辑的部门信息不存在，请查证');
        jobId = null;
    }
return {
    init(){
        let params = window.location.href.queryURLParams();
        if(params.hasOwnProperty('jobId')){
            jobId = params.jobId;
            queryBaseInfo();
        }
        $inpBoxName.blur(checkName);
        $inpBoxDesc.blur(checkDesc);
        $submit.click(submitHandle);
    }
}
})();
jobaddModule.init();
